package com.bgrc.hubu12305.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bgrc.hubu12305.common.CodeEnum;
import com.bgrc.hubu12305.common.Result;
import com.bgrc.hubu12305.common.utils.UUIDUtils;
import com.bgrc.hubu12305.entity.OrderEntity;
import com.bgrc.hubu12305.entity.RoleEntity;
import com.bgrc.hubu12305.entity.UserEntity;
import com.bgrc.hubu12305.service.AuthRelationService;
import com.bgrc.hubu12305.service.OrderService;
import com.bgrc.hubu12305.service.RoleService;
import com.bgrc.hubu12305.service.UserService;
import com.bgrc.hubu12305.vo.UserRegisterVO;
import com.bgrc.hubu12305.vo.UserReturnVO;
import com.bgrc.hubu12305.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 包名:com.bgrc.hubu12305.controller
 * 用户:86182
 * 时间:20211211:182021/12/9
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private AuthRelationService authRelationService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private OrderService orderService;

    @PostMapping("/login")
    @ApiOperation(value = "登录")
    public Result<UserReturnVO> login(@RequestBody UserVO userVO){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(userVO.getUserName(), userVO.getPwd());
        String sessionId =null;
        try {
            subject.login(token);
            if(subject.getSession()!=null){
                sessionId=(String) subject.getSession().getId();
                log.info("session=====>"+sessionId);
            }
        }catch (UnknownAccountException e){
            e.printStackTrace();
            log.info("用户名错误");
            return Result.build(null,CodeEnum.UNKNOWN_ACCOUNT);
        }catch (IncorrectCredentialsException e){
            e.printStackTrace();
            log.info("密码错误");
            return Result.build(null,CodeEnum.PASSWORD_ERROR);
        }
        //认证成功，返回登录对象
        //通过登录用户名找到实际表中的用户
        UserEntity userEntity = userService.findUserByName(userVO.getUserName());
        //得到用户的角色id
        long roleId = authRelationService.getRoleId(userEntity.getId());
        //获取角色信息
        RoleEntity roleEntity = roleService.queryRoleById(roleId);

        UserReturnVO userReturnVO = new UserReturnVO(
                        userEntity.getId(),
                        userEntity.getUserName(),
                        userEntity.getPwd(),
                        userEntity.getTel(),
                        userEntity.getGender(),
                        userEntity.getIdentityCard(),
                        roleEntity.getTitle(),
                        sessionId);

        return Result.ok(userReturnVO);
    }

    @GetMapping("/logout")
    @ApiOperation(value = "用户注销")
    public Result logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return Result.ok();
    }

    @GetMapping("/test")
    public Result test(){
        return Result.ok("hsy");
    }

    @ApiOperation(value = "注册")
    @PostMapping("/register")
    public Result<UserReturnVO> register(@RequestBody UserRegisterVO userRegisterVO){

        boolean b = userService.saveUser(userRegisterVO);
        if (b){
            return Result.ok();
        }
        return Result.build(null,CodeEnum.REPEATED_USER);
    }
    @ApiOperation(value = "查看订单")
    @GetMapping("/queryOrder")
    public Result queryOrder(@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage){

        Subject subject = SecurityUtils.getSubject();
        UserEntity user = (UserEntity) subject.getPrincipal();
        System.out.println(user.getUserName());
        List<OrderEntity> orderList = orderService.queryPage(currentPage, user.getUserName());
        return Result.ok(orderList);

    }
    @ApiOperation(value = "修改个人信息")
    @PostMapping("/modifyUser")
    public Result modify(@RequestBody UserEntity userEntity){

        boolean b = userService.updateById(userEntity);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }

    @ApiOperation(value = "修改密码")
    @PostMapping("/modifyPassword")
    public Result modifyPassword(@RequestParam(value = "newPwd") String pwd){

        boolean b = userService.modifyPasswordByName(pwd);
        if (b){
            return Result.ok();
        }
        return Result.fail();
    }



}
